using MySolution.Api.Dto;
using Mysolution.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace MySolution.Api.Controllers;

[ApiController]
[Route("api/[controller]")]

public class AuthController :ControllerBase
{
    private readonly AuthService _authService;

    public AuthController(AuthService authService)
    {
        _authService=authService;
    }
        [HttpPost("register")]
    public async Task<IActionResult> Register(UserRegisterDto userRegisterDto)
    {
        await _authService.RegisterAsync(userRegisterDto.Username, userRegisterDto.Password);
        return Ok("注册成功");
    }

    [HttpPost("login")]
    public async Task<IActionResult> Login(string username, string password)
    {
        var user = await _authService.LoginAsync(username, password);
        if (user == null) return Unauthorized("用户名或密码错误");
        return Ok("登录成功");
    }
}